﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GitCommitHelper
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.textBox1.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            var list = getEmptyFolderList(this.textBox1.Text, false);
            this.listBox1.Items.AddRange(list.ToArray());
        }

        private List<string> getEmptyFolderList(string path, bool addtxt)
        {
            List<string> result = new List<string>();
            var subfolders = Directory.GetDirectories(path);
            if (Directory.Exists(path))
            {
                if (Directory.GetFiles(path).Length == 0 && subfolders.Length == 0)
                {
                    if (addtxt)
                    {
                        using (var file = File.Open(Path.Combine(path, "empty.txt"), FileMode.OpenOrCreate))
                        {
                            file.Close();
                        }
                    }
                    result.Add(path);
                }
            }
            foreach (var f in subfolders)
            {
                result.AddRange(getEmptyFolderList(f, addtxt).ToArray());
            }
            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            var list = getEmptyFolderList(this.textBox1.Text, true);
            this.listBox1.Items.AddRange(list.ToArray());
        }
    }
}
